package pers.liu.yibandemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.liu.yibandemo.entity.Grade;
import pers.liu.yibandemo.service.GradeService;

import java.util.List;

@RestController
@RequestMapping("/grades")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @PostMapping("/add")
    public ResponseEntity<String> addGrade(@RequestBody Grade grade) {
        gradeService.addGrade(grade);
        return ResponseEntity.ok("Grade added successfully.");
    }

    @GetMapping("/all")
    public ResponseEntity<List<Grade>> getAllGrades() {
        List<Grade> grades = gradeService.getAllGrades();
        return ResponseEntity.ok(grades);
    }

    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<Grade>> getGradesByStudentId(@PathVariable String studentId) {
        List<Grade> grades = gradeService.getGradesByStudentId(studentId);
        return ResponseEntity.ok(grades);
    }

    @GetMapping("/semester/{semester}/{studentId}")
    public ResponseEntity<List<Grade>> getGradesBySemesterAndStudentId(@PathVariable String semester, @PathVariable String studentId) {
        List<Grade> grades = gradeService.getGradesBySemesterAndStudentId(semester, studentId);
        return ResponseEntity.ok(grades);
    }

    @GetMapping("/course/{course}/{studentId}")
    public ResponseEntity<List<Grade>> getGradesByCourseAndStudentId(@PathVariable String course, @PathVariable String studentId) {
        List<Grade> grades = gradeService.getGradesByCourseAndStudentId(course, studentId);
        return ResponseEntity.ok(grades);
    }
    @GetMapping("/page")
    public ResponseEntity<IPage<Grade>> getGradesByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        IPage<Grade> grades = gradeService.getGradesByPage(pageNum, pageSize);
        return ResponseEntity.ok(grades);
    }
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteGrade(@PathVariable Long id) {
        gradeService.deleteGrade(id);
        return ResponseEntity.ok("Grade deleted successfully.");
    }
    @PutMapping("/update")
    public ResponseEntity<String> updateGrade(@RequestBody Grade garde) {
        gradeService.updateGrade(garde);
        return ResponseEntity.ok("Grade updated successfully.");
    }
}
